package best.driver.android.View.photocrop;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import best.driver.android.AppConstant;
import best.driver.android.R;


/**
 * Created by xuefeng on 2014/7/11.
 */
public class SelectPhotoDialog {
    final Context context;
    public Dialog ad;

    Button button_takephoto;
    Button button_selectalbum;
    View divider;
    ImageView mIvExample;

    public void setListener(SelectPhotoListener listener) {
        this.listener = listener;
    }

    Button button_cancel;
    public SelectPhotoListener listener;

    public interface SelectPhotoListener{
        public void cancelSelect();
        public void selectTakePhoto();
        public void selectFromAlbum();
    }

    public SelectPhotoDialog(final Context context, int type) {

        this.context = context;
        ad = new Dialog(context, R.style.Dialog_Fullscreen);
        ad.setContentView(R.layout.activity_selectphoto_way);
		Window window = ad.getWindow();
		window.setGravity(Gravity.BOTTOM);
		

        //ad.requestWindowFeature(Window.FEATURE_NO_TITLE);
//        ViewGroup.LayoutParams lay = ad.getWindow().getAttributes();
//        setParams(lay);

        ad.setOwnerActivity((Activity) context);
        ad.show();
        ad.setCancelable(true);
        ad.setCanceledOnTouchOutside(true);
        window.findViewById(R.id.layout_dialog).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				dismiss();
			}
		});
        
        //替换对话框窗口的布局
        button_selectalbum = (Button) window.findViewById(R.id.btn_select_album);
        button_takephoto = (Button) window.findViewById(R.id.btn_select_takephoto);
        divider = (View) window.findViewById(R.id.divider);
        mIvExample = (ImageView) window.findViewById(R.id.iv_example);
        button_cancel = (Button) window.findViewById(R.id.btn_select_cancel);
        button_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(null != listener) {
                    listener.cancelSelect();
                    ad.dismiss();
                }
            }
        });

        button_selectalbum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.selectFromAlbum();
                ad.dismiss();
            }
        });

        button_takephoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.selectTakePhoto();
                ad.dismiss();
            }
        });

        switch (type) {
            case AppConstant.PHOTO_CERTIFICATION_FRONT:
                mIvExample.setVisibility(View.VISIBLE);
                mIvExample.setImageResource(R.drawable.certification_front);
                break;
            case AppConstant.PHOTO_CERTIFICATION_BACK:
                mIvExample.setVisibility(View.VISIBLE);
                mIvExample.setImageResource(R.drawable.certification_back);
                break;
            case AppConstant.PHOTO_DRIVER:
                mIvExample.setVisibility(View.VISIBLE);
                mIvExample.setImageResource(R.drawable.driver_phont);
                break;
            case AppConstant.PHOTO_ON_ROAD:
                mIvExample.setVisibility(View.VISIBLE);
                mIvExample.setImageResource(R.drawable.onroad_photo);
                break;
            default:
                mIvExample.setVisibility(View.GONE);

        }
        
    }

    public void setAlbumVisible() {
        button_selectalbum.setVisibility(View.VISIBLE);
        divider.setVisibility(View.VISIBLE);
    }


    public void dismiss() {
        if(ad != null && ad.isShowing()) {
            ad.dismiss();
        }
    }

}
